#include "main.h"
#include "global.h"
#include <time.h>

#define N_SIZES 10

static void *font = 0;
static int color_min = 0;
static int color_max = 9;
static GLfloat rate_scale = 1.0;
static GLfloat color_scale = 0.65;
static unsigned int num_servers = 0;
static GLfloat lindot_scale = 5.0;
static GLfloat mindot_size = 0.0;


void dpl_point(float ax, float ay, float az, float nb){
  int red=0, green=0, blue=255;
  if (nb > 100 && nb < 200){
    red=0;
    green=255;
    blue=0;
  } else if (nb >199 && nb < 500){
    red=255;
    green=0;
    blue=0;
  }

 glBegin(GL_POINTS);
  glColor3f(red, green, blue);
  glVertex3f(ax,ay,az);
  glEnd();
}

void idle(void) {
  glutPostRedisplay();
}

void
set3DMode()
{
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluLookAt(0.0f, 0.0f, 300.0f, // position oeil
           0.0f,0.0f,1.0f, // point regardé
              0.0f,1.0f,0.0f); // vecteur haut
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}


void
set2DMode()
{
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, height, 0, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

void dpl_trace_points(){
 int i;
 //printf("Nombre d'éléments: %i\n", count);
 glNewList(2,GL_COMPILE);
 for (i=0;i<count;i++){
	//printf("%i %i %i\n",myData[i]->dec_ipv4,myData[i]->src_port,myData[i]->dst_port);
   //dpl_point(x[i],y[i],z[i]);
   dpl_point(myData[i].dec_ipv4, myData[i].src_port, myData[i].dst_port, myData[i].nb);
 }
 glEndList();
}

GLfloat
pointSize(unsigned int s)
{
  return lindot_scale * (s+1) + mindot_size;
}

void
drawStr(int x, int y, const char *str)
{
        if (!str) return;
        if (y < 0) y += height;
        glRasterPos2d(x, y);
        while (*str) {
                glutBitmapCharacter(font, *str++);
        }
}
void drawStr3d(int x, int y, int z, const char *str){
  if (!str) return;
  if (y < 0) y += height;
    glRasterPos3d(x, y, z);
    while (*str) {
      glutBitmapCharacter(font, *str++);
    }
}

void
drawText(void)
{
        char buffer[256];
  unsigned int s;
  time_t theTime = 0.0;
        font = GLUT_BITMAP_9_BY_15;
        glColor3f(0.7, 0.7, 0.7);
  unsigned int n = 1;
  unsigned int save_n = 0;
  unsigned int y=-350;
  unsigned int l=500;
        
        snprintf(buffer, sizeof(buffer), "Paquets: %i", count);
        drawStr(y, n*15, buffer);
        n=n+l;
}

/*
 * Fonction affichage du rectangle
*/
void dpl_rectangle(){
  font = GLUT_BITMAP_8_BY_13;
  drawStr3d(-0.0, -32767.0, 32768.0, "IPv4");
  glColor3f( 1,1,1 );
  glBegin( GL_LINE_LOOP ); /* face avant */
  glVertex3f( -127.0, -32767.0,  32768.0 );
  glVertex3f(  127.0, -32767.0,  32768.0 );
  glVertex3f(  127.0,  32768.0,  32768.0 );
  glVertex3f( -127.0,  32768.0,  32768.0 );
  glEnd();

  glBegin( GL_LINE_LOOP ); /* face arriere */
  glVertex3f( -127.0, -32767.0,  -32767.0 );
  glVertex3f(  127.0, -32767.0,  -32767.0 );
  glVertex3f(  127.0,  32768.0,  -32767.0 );
  glVertex3f( -127.0,  32768.0,  -32767.0 );
  glEnd();

  drawStr3d(-127.0, 0.0, 32768, "Dst Port");
  drawStr3d(127.0, -32767.0, 0, "Src Port");
  glBegin( GL_LINES ); /* autres aretes */
  glVertex3f( -127.0, -32767.0,  32768.0 );
  glVertex3f( -127.0, -32767.0, -32767.0 );

  glVertex3f(  127.0, -32767.0,  32768.0 );
  glVertex3f(  127.0, -32767.0, -32767.0 );

  glVertex3f(  127.0,  32768.0,  32768.0 );
  glVertex3f(  127.0,  32768.0, -32767.0 );

  glVertex3f( -127.0,  32768.0,  32768.0 );
  glVertex3f( -127.0,  32768.0, -32767.0 );
  glEnd();
}

void
drawAxis(void)
{
        glBegin(GL_LINES);
        glColor4f(1, 0, 1, 1);
        glVertex3i(0, 0, -1);
        glVertex3i(0, 0, 1);
        glVertex3i(0, -1, 0);
        glVertex3i(0, 1, 0);
        glVertex3i(-1, 0, 0);
        glVertex3i(1, 0, 0);
        glEnd();

        glColor3f(1, 1, 1);
        glRasterPos3f(1, 0, 0);
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, 'x');
        glRasterPos3f(0, 1, 0);
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, 'y');
        glRasterPos3f(0, 0, 1);
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, 'z');
}

/* Affichage de la scene */
void display()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  glEnable(GL_POINT_SMOOTH);
//  set3DMode();
  /* Initialiser les transformations geometriques */
  //glMatrixMode(GL_PROJECTION);
  //glLoadIdentity();
  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();
  gluPerspective (50.0*zoomFactor, (float)width/(float)height, 0, 0); 

  /* Repere de la camera ici */
  gluLookAt(0.0f, 0.0f, 300.0f, // position oeil
   0.0f,0.0f,1.0f, // point regardé
   0.0f,1.0f,0.0f); // vecteur haut

  /* Placer la scene par rapport a la camera */
  glTranslatef( tb_translation[0], tb_translation[1], tb_translation[2] );
  glMultMatrixf( tb_rotation );
//  drawAxis();
  
  glNewList(1,GL_COMPILE); 	// Nouvelle liste d'objets
	
  /* Placer un objet */
  glTranslatef( 0, 0, 0 );
  glRotatef( 0, 0,1,0 );
  glScalef( 1, 0.003, 0.003 ); 

  dpl_rectangle();		// Affichage rectangle
//  dpl_point(0,0.5,0.5);		// Affichage d'un point

  glEndList();
  glCallList(1);
  dpl_trace_points();
  glCallList(2);
  //set2DMode();
  drawText();
  //glPushMatrix();

  /* Afficher le buffer */
  glutSwapBuffers();
  //glutPostRedisplay();
}
